package com.weiweiqin.controller.home;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weiweiqin.model.Comment;
import com.weiweiqin.model.Customer;
import com.weiweiqin.service.CommentService;
import com.weiweiqin.service.GoodsCommentItemService;
import com.weiweiqin.vo.common.CommentVO;
import com.weiweiqin.vo.common.Page;
import com.weiweiqin.vo.common.Result;

@Controller
@RequestMapping("front/comment")
public class FCommentController {

	private static final Logger logger = LoggerFactory
			.getLogger(FCommentController.class);
	@Autowired
	private CommentService commentService;

	@Autowired
	private GoodsCommentItemService goodsCommentItemService;

	@RequestMapping(value = "/addByCoustomer.do", method = {
			RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String addByCoustomer(CommentVO commentVO, HttpSession session) {
		logger.info("Enter method addByCoustomer...,commentVO=" + commentVO);
		Result result = new Result();
		try {
			if (null != session.getAttribute("customer")) {
				Customer customer = (Customer) session.getAttribute("customer");
				Comment comment = commentVO.getComment();
				if (null != comment) {
					if (commentVO.getNiming() == 1) {
						if (StringUtils.isEmpty(customer.getNickname())) {
							comment.setUsername("匿名用户");
						} else {
							String nickname = customer.getNickname();

							if (nickname.length() == 1) {
								String s = nickname.substring(0, 1);
								nickname = s + "***" + s + "（匿名）";
							} else {
								String s = nickname.substring(0, 1);
								String lastS = nickname.substring(
										nickname.length() - 1,
										nickname.length());
								nickname = s + "***" + lastS + "（匿名）";
							}
							comment.setUsername(nickname);
						}
					} else {
						if (StringUtils.isEmpty(customer.getNickname())) {
							comment.setUsername("匿名用户");
						} else {
							comment.setUsername(customer.getNickname());
						}
					}
					// 待审核
					comment.setStatus(1);
					comment.setUpdateDate(new Date());
					commentService.addByCoustomer(comment,
							commentVO.getGoodsIds());
				}

			}

		} catch (Exception e) {
			logger.error("发表评论失败...", e);
			result.setResult(false);
			result.setErrorMsg("发表评论失败...");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}

	    

	@RequestMapping(value = "/pageListForHome.do", method = {
			RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String pageListForHome(int goodsId, int currPage) {
		Result result = new Result();
		Page page = new Page();
		page.setCurrPage(currPage);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("goodsId", goodsId);
		conditions.put("status", 0);
		List<Comment> commentList = commentService.pageList(page, conditions);
		int totalCount = commentService.totalCount(conditions);
		page.setTotalCount(totalCount);
		List<CommentVO> commentVOList = new ArrayList<CommentVO>();
		if (null != commentList && !commentList.isEmpty()) {
			for (Comment comment : commentList) {
				CommentVO vo = new CommentVO();
				vo.setComment(comment);
				String date = sdf.format(comment.getUpdateDate());
				vo.setUpdateDate(date);
				commentVOList.add(vo);
			}
		}
		Map<String, Object> mp = new HashMap<String, Object>();
		mp.put("totalPage", page.getTotalPage());
		mp.put("totalCount", totalCount);
		mp.put("list", commentVOList);
		result.setObj(mp);
		result.setResult(true);
		return result.toJson();
	}

}